#include "indexguide.h"
#include <QDebug>

using namespace Moolink;




bool IndexGuide::setDataProcessFun(Device *device)
{
    bool foundit=true;
    QByteArray ba=("lib"+device->protocolLib).toLatin1();
    const char *protocol_char=ba.data();
    QMetaEnum metaEnum=QMetaEnum::fromType<protocollibList>();
    protocollibList proInt=protocollibList(metaEnum.keyToValue(protocol_char));


    switch (proInt) {
    case libFlowmeter:
        device->dataProcessFun = &Flowmeter::getDataResultStr;
        device->protocolLibDataIsOk=true;
    break;
    case libModbusTest:
        device->dataProcessFun = &ModbusTest::getDataResultStr;
        device->protocolLibDataIsOk=true;
    break;

    case libADCmodule:
        device->dataProcessFun = &ADCmodule::getDataResultStr;
        device->protocolLibDataIsOk=true;
    break;




    default:
        device->dataProcessFun = &IndexGuide::nullgetDataResultStr;
        device->protocolLibDataIsOk=false; foundit=false;
    break;
    }
    return foundit;
}


bool IndexGuide::setCmdProcessFun(Device *device)
{
    bool foundit=true;
    QByteArray ba=("lib"+device->protocolLib).toLatin1();
    const char *protocol_char=ba.data();
    QMetaEnum metaEnum=QMetaEnum::fromType<protocollibList>();
    protocollibList proInt=protocollibList(metaEnum.keyToValue(protocol_char));


    switch (proInt) {
    case libFlowmeter:
        device->cmdProcessFun = &Flowmeter::getReadCmd;
        device->protocolLibCmdIsOk=true;
    break;
    case libModbusTest:
        device->cmdProcessFun = &ModbusTest::getReadCmd;
        device->protocolLibCmdIsOk=true;
    break;
    case libADCmodule:
        device->cmdProcessFun = &ADCmodule::getReadCmd;
        device->protocolLibCmdIsOk=true;
    break;


    default:
        device->cmdProcessFun = &IndexGuide::nullgetReadCmd;
        device->protocolLibCmdIsOk=false; foundit=false;
    break;
    }
    return foundit;
}




QString (*IndexGuide::getDataProcessFun(QString protocol))(Device *device,QByteArray responseData)
{
    QByteArray ba=("lib"+protocol).toLatin1();
    const char *protocol_char=ba.data();
    QMetaEnum metaEnum=QMetaEnum::fromType<protocollibList>();
    protocollibList proInt=protocollibList(metaEnum.keyToValue(protocol_char));


    switch (proInt) {
    case libFlowmeter:
        return &Flowmeter::getDataResultStr;
    break;
    case libModbusTest:
        return &ModbusTest::getDataResultStr;
    break;

    default:
        return &IndexGuide::nullgetDataResultStr;
    break;
    }

    return &IndexGuide::nullgetDataResultStr;
}

//QByteArray (*IndexGuide::getCmdProcessFun(QString protocol))(Device *device,int funCode)
//{
//    QByteArray ba=("lib"+protocol).toLatin1();
//    const char *protocol_char=ba.data();
//    QMetaEnum metaEnum=QMetaEnum::fromType<protocollibList>();
//    protocollibList proInt=protocollibList(metaEnum.keyToValue(protocol_char));


//    switch (proInt) {
//    case libFlowmeter:
//        return &Flowmeter::getReadCmd;
//    break;

//    case libModbusTest:
//        return &ModbusTest::getReadCmd;
//    break;

//    default:
//        return &IndexGuide::nullgetReadCmd;
//    break;
//    }

//    return &IndexGuide::nullgetReadCmd;
//}



QString IndexGuide::nullgetDataResultStr(Device *device,QByteArray responseData)
{
    QString ResultStr="Error:DataProcessFun Not Found"; return ResultStr;
}

QByteArray IndexGuide::nullgetReadCmd(Device *device,int funCode)
{
    QString ResultStr="\0"; return ResultStr.toLatin1();
}
